package com.car.web.controller.upload;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.car.util.Const;
import com.ssi.utils.AjaxUtils;
/**
 * @author geyang    
 * @version 1.0  
 * @created 2014年12月24日 下午3:57:30
 */


@Controller
@RequestMapping("/sys")
public class uploadController  {
	private static final Logger logger = Logger.getLogger(uploadController.class);
	
	
	
	

	@RequestMapping(value = "/upload")
	public void upFile(@RequestParam(value="file", required=false) MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws IOException{
		Map<String,String> jsonMap = new HashMap<String,String>();
		String realFileName = uploadPic(request, file, Const.uploadImageDire, Const.runInProduct);
		boolean fileInDB = false;
		if(!StringUtils.isEmpty(realFileName)){
			fileInDB = true;
			jsonMap.put("fileInDB", fileInDB?"true":"false");
			jsonMap.put("filename",realFileName);
		}
		AjaxUtils.ajaxJson(response, jsonMap);
	}
	/**
	 * 上传图片
	 * @param request
	 * @param file 文件 
	 * @param iconDirectory 文件相对目录
	 * @param runInProduct 运行到生产
	 * @return
	 */
	public static String uploadPic(HttpServletRequest request, MultipartFile file, String iconDirectory,
			Boolean runInProduct) {
		String realFileName = "";
		String path = "";
		String ctxPath = "";
		if ((file != null) && (!"".equals(file.getOriginalFilename()))) {
			// realFileName = new Date().getTime() + "_" +
			// file.getOriginalFilename();
			String originlType = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));

			String video = "*.rm;*.rmvb;*.avi;*.3gp;*.mp4;*.mkv;*.flv;*.wmv;.mp3";
			String image = ".jpg;.gif;.jpeg;.png;.bmp";
			String document = "*.zip;*.rar;*.csv;*.xls;*.xlsx;*.doc;*.ppt;*.txt;*.pdf;";
			if (video.contains(originlType)) {
				path = Const.uploadVideoDire;
			} else if (image.contains(originlType)) {
				path = Const.uploadImageDire;
			} else if (document.contains(originlType)) {
				path = Const.uploadWordDire;
			} else {
				path = Const.uploadWordDire;
			}
			// String ctxPath = Const.getUploadFilePath(request, path,
			// Const.runInProduct);
			ctxPath = "/mydata/" + path + "/";
			realFileName = getNonceStr() + "_" + getCurrTime() + originlType;

			File dirPath = new File(ctxPath);
			if (!dirPath.exists()) {
				dirPath.mkdirs();
			}
			logger.error("path:" + ctxPath + realFileName);

			try {
				File uploadFile = new File(ctxPath + realFileName);
				if ((file.getOriginalFilename() != null) && (!"".equals(file.getOriginalFilename())))
					file.transferTo(uploadFile);
			} catch (Exception e) {
				e.printStackTrace();
				return "";
			}
		}
		return ctxPath + realFileName;
	}
	public static int getNonceStr() {
		Random random = new Random();
		return random.nextInt(10000);
		
		
	}
	public static String getCurrTime() {
		Date now = new Date();
		SimpleDateFormat outFormat = new SimpleDateFormat("yyyyMMddHHmmss");
		String s = outFormat.format(now);
		return s;
	}
}
